#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

REPO_ROOT="$(git rev-parse --show-toplevel)"
SRC_DIR=${REPO_ROOT}/modelserving
cd "${SRC_DIR}"

# Pick a probably-unique tag
export TAG=`date +%Y%m%d%H%M%S`

# If we're building for kind, default to only building on cpu
if [[ -z "${ARCHITECTURES:-}" ]]; then
  ARCHITECTURES=cpu
  export ARCHITECTURES
fi

if [[ -z "${KUBE_CONTEXT:-}" ]]; then
  KUBE_CONTEXT=kind-kind
  echo "Defaulting to kube context: ${KUBE_CONTEXT}"
fi

# Build the image
echo "Building images"
export IMAGE_PREFIX=fake.registry/
BUILDX_ARGS=--load dev/tasks/build-images

MODEL_IMAGE="${IMAGE_PREFIX:-}llamacpp-gemma3-12b-it-cpu:${TAG}"
RPCSERVER_IMAGE="${IMAGE_PREFIX:-}rpc-server-cpu:${TAG}"

# Load the image into kind
echo "Loading images into kind: ${MODEL_IMAGE}, ${RPCSERVER_IMAGE}"
kind load docker-image ${MODEL_IMAGE} ${RPCSERVER_IMAGE}

# Deploy manifests
echo "Deploying manifests"
cat k8s/llm-server-cpu.yaml | sed s@llamacpp-gemma3-12b-it-cpu:latest@${MODEL_IMAGE}@g | \
  kubectl apply --context=${KUBE_CONTEXT} --server-side -f -

cat k8s/rpc-server-cpu.yaml | sed s@rpc-server-cpu:latest@${RPCSERVER_IMAGE}@g | \
  kubectl apply --context=${KUBE_CONTEXT} --server-side -f -

cat k8s/llm-server-rpc.yaml | sed s@llamacpp-gemma3-12b-it-cpu:latest@${MODEL_IMAGE}@g | \
  kubectl apply --context=${KUBE_CONTEXT} --server-side -f -
